<?php
namespace Home\Controller;
use Home\Model\GoodsModel;
use Home\Model\CategoryGoodsModel;
class GoodsController extends HomeController{
    public function index(){     
        $where['status'] = 1;
        $GoodsModel = new GoodsModel();
        $sort = I('sort');
        $type= I('type');
        if (empty($sort)){
            $sort = 'create_time';
        }else{
            if(session('type')=='desc'){
                session('type','asc');
            }else {
                session('type','desc');
            }
        }
        $type = session('type');
         
        $goods = $this->lists($GoodsModel,$where,array($sort=>$type));
        
        $this->assign('goods',$goods);      
        $this->assign('sort',$sort);
        $this->assign('type',$type);
        if(IS_AJAX){
            $result['p']=I('get.p')+1;
            $result['content']=$this->fetch('ajaxgoodslist');
            $result['errno']=0;
            $this->ajaxReturn($result);
        }
        $this->setSiteTitle('商品');
        $this->display();
    }
    public function cate($id=0){
        $where['id'] = $id;
        $CategoryModel = new CategoryGoodsModel();
      
        $cate = $CategoryModel->where($where)->find();
       
        if(!$cate) $this->error ('栏目不存在');
        
        unset($where);
        $cateId= I('get.id','','intval');
        $where['status'] = 1;
        $in = D('CategoryGoods')->getChildrenId($cateId);
        $where['cate_id'] = array('in',$in);
        $GoodsModel = new GoodsModel();
        $sort = I('sort');
        $type= I('type');
        if (empty($sort)){
            $sort = 'create_time';          
        }else{
           if(session('type')=='desc'){
               session('type','asc');
           }else {
               session('type','desc');
           } 
        }
        $type = session('type'); 
        //商品列表 
        $goods = $this->lists($GoodsModel,$where,array($sort=>$type));
        foreach ($goods as $k=>$v){  
            $goods[$k]['url'] = U('goods/info',array('id'=>$v['id']));
        }
        $this->assign('goods',$goods);
        
        //推荐商品
        $recommendGoods = $GoodsModel->recommendGoods();
        $this->assign('recommend_goods',$recommendGoods);
        
        //子分类
        unset($where);
       
        if($cate['pid'] == 0){
            $where['id'] = array('in',$in);
            $childrenTree = $this->listAll('CategoryGoods',$where,' pid asc');
        }else {
            $parent = $CategoryModel->info($cate['pid']);
            while ($parent['pid']!=0){
                $parent = $CategoryModel->info($parent['id']);
               
            }
            unset($where);
            $in = $CategoryModel->getChildrenId($parent['id']);
            $where['id'] = array('in',$in);
            $childrenTree = $this->listAll('CategoryGoods',$where,' pid asc');
            
        }
        $this->assign('url',U('cate',array('id'=>$id)));
        $this->assign('children_tree',$childrenTree);
        $this->assign('cate',$cate);
        $this->assign('sort',$sort);
        $this->assign('type',$type);
        $this->setSiteTitle($cate['category_name']);
        if(IS_AJAX){
            $result['p']=I('get.p')+1;
            $result['content']=$this->fetch('ajaxgoodslist');
            $result['errno']=0;
            $this->ajaxReturn($result);
        }
        $this->display();
    }
    
    public function info(){
        $id = I('get.id','','intval');
       
        
        $goods = D('Goods')->info($id);
        if($goods){
            M('goods')->where('id='.$id)->setInc('hits');         
            $hot_goods = D('goods')->hotGoods($goods['cate_id']);
            $this->assign('hot_goods',$hot_goods);
            $this->assign('goods',$goods);
            if(!empty($goods['seo_title'])){
                $this->setSiteTitle($goods['seo_title']);
            }else {
            $this->setSiteTitle($goods['name']);
            }
            $this->display();   
        }else{
            $this->error('商品不存在');
        }
    }
    public function buttonInfo(){
        $id = I('post.id','','intval');
        $result = array(
            'errno' => 0,
            'content' => '',
            'message' => ''
        );
        if($id > 0){            
            $info = D('Goods')->info($id); 
            if($info){
               
                $goods['src'] = get_image_url($info['pic_url']);
                $goods['price'] = $info['discount_price'] > 0  ? $info['discount_price'] : $info['price'];
                $goods['url']   = U('Goods/info?id='.$info['id']);
                $goods['name'] = $info['name'];
                $goods['description'] = $info['description'];
                $result['content'] = $goods;
            }else {
                $result['errno'] = 2;
                $result['content'] = '未找到该商品';
            }
           
        }else {
            $result['errno'] = 1;
            $result['content'] = '数据错误';
            
        }
        $this->ajaxReturn($result);
    }
    public function ajGetGoodsDetial(){
       
            $url = I('url');
            $data = file_get_contents($url);
            preg_match('/dsc\.taobaocdn\.com[\w\.\%\/]*[\"]/i', $data,$descurl);
            $descurl = "http://".trim($descurl[0],'"');
            $content = file_get_contents($descurl);
            
            if($content){
                $content = mb_convert_encoding($content, 'utf-8', 'GBK,UTF-8,ASCII');
                $content = trim($content,"var desc='");
                $where['item_url'] = htmlspecialchars_decode($url);
                $save['item_body'] = $content;
                $save['item_body'] = substr($save['item_body'],0,-3);
                M('goods')->where($where)->save($save);
                $result['status']= 1;
                $result['content'] = $content;
                $this->ajaxReturn($result);
            }else{
               
                $this->ajaxReturn("商品详情获取失败，请刷新重试");
            }
            
        
    }
 
    public function tag($id){
        if(!is_numeric($id) || $id <= 0){
            $this->error('非法的标签');
        }  
        $where['status'] = 1;
        $where['tag']    = $id;
        $info = D('Tag')->info($id);
        $list = $this->lists(D('Goods'),$where);
    
        $this->assign('goods',$list);
        $this->setSiteTitle($info['tag_name']);
        $this->display();
    }
    public function ajaxGoodsInfo($id){
        $GoodsModel = new GoodsModel();
        $goods = $GoodsModel->info($id);
        $result = array(
            'errno' =>0,
            'content' =>'',
            'message' => ''    
        );
        if(!empty($goods)){
           $goods['url'] = U('Goods/info',array('id'=>$goods['id']));
           $result['content'] = $goods;
        }else {
            $result['errno'] = 1;
            $result['message'] = '无效的商品';
        }
        $this->ajaxReturn($result);
    }
}